package com.lancoo.znbkxx.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lancoo.znbkxx.R;
import com.lancoo.znbkxx.beans.SamplePaperBean;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Create by TangJun on 2019/2/16
 */
public class SearchAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static final int VIEW_TYPE_SEARCH_J = 0;
    public static final int VIEW_TYPE_SEARCH_G = 1;
    public static final int VIEW_TYPE_SEARCH_M = 2;
    private Context context;
    private List<SamplePaperBean> mData;
    private StringBuilder keyString;

    public SearchAdapter(Context context,List<SamplePaperBean> data) {
        this.context = context;
        mData = data;
        keyString = new StringBuilder();
    }

    public void setKeyString(String key) {
        if (TextUtils.isEmpty(keyString.toString())) {
            keyString.append(key);
        } else {
            keyString.setLength(0);
            keyString.append(key);
        }
    }

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        RecyclerView.ViewHolder myViewHolder = null;
        switch (i){
            case VIEW_TYPE_SEARCH_J:
                myViewHolder =  new NotDownVH(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_search_j, parent, false));
                break;
            case VIEW_TYPE_SEARCH_G:
                myViewHolder =  new NotDownVH(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_search_g, parent, false));
                break;
            case VIEW_TYPE_SEARCH_M:
                myViewHolder =  new NotDownVH(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_search_m, parent, false));
                break;
        }
        return myViewHolder;
    }
    @Override
    public int getItemViewType(int position) {
        SamplePaperBean samplePaperBean = mData.get(position);
        if (samplePaperBean.getLayer() == 0) {
            return VIEW_TYPE_SEARCH_J;
        } else if (samplePaperBean.getLayer() == 1) {
            return VIEW_TYPE_SEARCH_J;
        } else {
            return VIEW_TYPE_SEARCH_J;
        }
    }

    static class NotDownVH extends RecyclerView.ViewHolder {
        TextView nameTv;

        NotDownVH(View itemView) {
            super(itemView);
            nameTv = itemView.findViewById(R.id.tv_item);
        }
    }
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        SamplePaperBean bean = mData.get(position);
        final String paperName = bean.getPaperName();
        NotDownVH notDownVH = (NotDownVH)holder;
        try {
            notDownVH.nameTv.setText(matcherSearchText(paperName));
        } catch (Exception e) {
            notDownVH.nameTv.setText(paperName);
        }
        if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //int pos = holder.getLayoutPosition();
                }
            });
        }
    }

    @SuppressLint("ResourceAsColor")
    private SpannableString matcherSearchText(String text) {
        String string = text.toLowerCase();
        String key = keyString.toString().toLowerCase();
        Pattern pattern = Pattern.compile(key);
        Matcher matcher = pattern.matcher(string);
        SpannableString ss = new SpannableString(text);
        while (matcher.find()) {
            int start = matcher.start();
            int end = matcher.end();
            ss.setSpan(new ForegroundColorSpan(Color.rgb(0,153,255)), start, end,
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return ss;
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }
}


